simpson's 1-3rd rule Algorithm

The Simpson's 1/3rd Rule Algorithm is a numerical integration technique that is used to approximate the definite integral of a continuous, smooth function over a certain interval. This rule is an improvement over the basic trapezoidal rule, as it provides a more accurate approximation by considering more points along the curve of the function. The method gets its name from the fact that it uses parabolic arcs to approximate the area under the curve, and the coefficients of the terms are in the ratio 1:3. In the algorithm, the interval between the points being integrated is divided into an even number of subintervals, with each subinterval having equal width. The function values at the endpoints of each subinterval are then used to construct parabolic arcs, which are then used to approximate the area under the curve. The total area is then calculated as the sum of the areas of these parabolic arcs, and this sum serves as the approximation of the definite integral. Simpson's 1/3rd Rule is particularly useful for functions that exhibit quadratic behavior, as the approximation error is reduced significantly compared to the trapezoidal rule.
#include<stdio.h>
#include<math.h> 

float f(float x) 
{
	return 1.0+x*x*x; //This is the expresion of the function to integrate?
}

void main()
{
	int i,n;
	float a,b,h,x,s2,s3,sum,integral;
	
	printf("enter the lower limit of the integration:");
	scanf("%f",&a);
	printf("enter the upper limit of the integration:");
	scanf("%f",&b);
	printf("enter the number of intervals:");
	scanf("%d",&n);
	
	h=(b-a)/n;
	sum=f(a)+f(b);
	s2=s3=0.0;

  for(i=1;i<n;i+=3)
	{
		x=a+i*h;
		s3=s3+f(x)+f(x+h);
	}

  for(i=3;i<n;i+=3)
	{
		x=a+i*h;
		s2=s2+f(x);
	}
	
	integral=(h/3.0)*(sum+2*s2+4*s3);
	printf("\nValue of the integral = %9.4f\n",integral);
	
	return 0;
}

LANGUAGE:

DARK MODE: